package com.projectCommon.util.commonUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;

//在这儿写的是一个静态方法。
public class ObjComparators {
    @SafeVarargs
    public static <R, T> boolean compare(T oldObj, T newObj, ObjCompareFunction<R, T>... objFunctions) {

        if (oldObj == null || newObj == null) {
            throw new RuntimeException("传入对象为空");
        }
        if (oldObj.getClass() != newObj.getClass()) {
            throw new RuntimeException("类型不相同");
        }
        if (objFunctions.length <= 0) {
            throw new RuntimeException("你要做什么");
        }

        for (ObjCompareFunction<R, T> objFunction : objFunctions) {
            R res1 = objFunction.apply(oldObj);
            R res2 = objFunction.apply(newObj);
            if (!res1.equals(res2)) {
                return false;
            }
        }
        return true;
    }

    // 比较两个不同对象的  不同字段进行比较。
    public static <T, Y> CompareRes compare(T obj1, Y obj2, Map<ObjCompareFunction<Object, T>, ObjCompareFunction<Object, Y>> maps) {
        if (obj1 == null || obj2 == null) {
            throw new RuntimeException("传入对象为空");
        }
        if (maps.size() <= 0) {
            throw new RuntimeException("传入的map有问题。");
        }
        CompareRes compareRes = new CompareRes(true, new ArrayList<String>());
        for (Map.Entry<ObjCompareFunction<Object, T>, ObjCompareFunction<Object, Y>> objCompareFunctionObjCompareFunctionEntry : maps.entrySet()) {
            ObjCompareFunction<Object, T> key = objCompareFunctionObjCompareFunctionEntry.getKey();
            ObjCompareFunction<Object, Y> value = objCompareFunctionObjCompareFunctionEntry.getValue();
            Object res1 = key.apply(obj1);
            Object res2 = value.apply(obj2);
            if (res1 == null && res2 == null) {
                continue;
            }
            if (res1 == null) {
                compareRes.setaBoolean(false);
                compareRes.getDiffs().add("null = > " + res2);
                continue;
            }
            if (!res1.equals(res2)) {
                BigDecimal val1 = convertToBigDecimal(res1);
                BigDecimal val2 = convertToBigDecimal(res2);
                if (val1 == null || val2 == null) {
                    compareRes.setaBoolean(false);
                    compareRes.getDiffs().add(res1 + " = > " + res2);
                    continue;
                }
                if (!(val1.compareTo(val2) == 0)) {
                    compareRes.setaBoolean(false);
                    compareRes.getDiffs().add(res1 + " = > " + res2);
                }
            }
        }
        return compareRes;
    }

    private static BigDecimal convertToBigDecimal(Object obj) {
        if (obj instanceof Number) {
            return new BigDecimal(obj.toString());
        } else if (obj instanceof Character) {
            char ch = (Character) obj;
            if (Character.isDigit(ch)) {
                return new BigDecimal(String.valueOf(ch));
            }
        } else if (obj instanceof String) {
            try {
                return new BigDecimal((String) obj);
            } catch (NumberFormatException e) {
                // 如果字符串不是有效的数字，则返回null
                return null;
            }
        }
        return null;
    }
}
